package cn.com.basic.framework.aliyun.oss;

import cn.hutool.core.lang.Assert;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.lang3.StringUtils;

/**
 * oss管理器
 *
 * @author zhuxj
 */
public class OSSManager {

    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;

    private OSS oss;

    private OSSManager() {
    }

    public static OSSManager newInstance() {
        return new OSSManager();
    }

    public void shutDownOssClient() {
        Assert.isTrue(this.oss != null, "oss client cannot be null");
            this.oss.shutdown();
    }

    public OSSManager newOssClient() {
        if (this.oss == null) {
            Assert.isTrue(StringUtils.isNotBlank(this.endpoint), "endpoint cannot be null or blank");
            Assert.isTrue(StringUtils.isNotBlank(this.accessKeyId), "accessKeyId cannot be null or blank");
            Assert.isTrue(StringUtils.isNotBlank(this.accessKeySecret), "accessKeySecret cannot be null or blank");
            this.oss = new OSSClientBuilder().build(this.endpoint, this.accessKeyId, this.accessKeySecret);
        }
        return this;
    }

    public OSSManager newOssClient(ClientBuilderConfiguration conf) {

        if (this.oss == null) {
            Assert.isTrue(StringUtils.isNotBlank(this.accessKeySecret), "accessKeySecret cannot be null or blank");
            Assert.isTrue(StringUtils.isNotBlank(this.endpoint), "endpoint cannot be null or blank");
            Assert.isTrue(StringUtils.isNotBlank(this.accessKeyId), "accessKeyId cannot be null or blank");
            this.oss = new OSSClientBuilder().build(this.endpoint, this.accessKeyId, this.accessKeySecret, conf);
        }
        return this;
    }

    public OSSManager newOssClient(String secretToken) {
        if (this.oss == null) {
            Assert.isTrue(StringUtils.isNotBlank(this.accessKeyId), "accessKeyId cannot be null or blank");
            Assert.isTrue(StringUtils.isNotBlank(this.endpoint), "endpoint cannot be null or blank");
            Assert.isTrue(StringUtils.isNotBlank(this.accessKeySecret), "accessKeySecret cannot be null or blank");
            Assert.isTrue(StringUtils.isNotBlank(secretToken), "secretToken cannot be null");
            this.oss = new OSSClientBuilder().build(this.endpoint, this.accessKeyId, this.accessKeySecret, secretToken);
        }
        return this;
    }

    public OSSManager endpoint(String endpoint) {
        this.endpoint = endpoint;
        return this;
    }

    public OSSManager accessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
        return this;
    }

    public OSSManager accessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
        return this;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public OSS getOss() {
        return this.oss;
    }
}